package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    /**
     * 通过username在user表中查询用户信息
     * 通过用户id在用户角色表和角色表查询角色信息
     * 通过角色id查找对应的权限
     */
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if (user == null) return null;
        Integer id = user.getId();
        Set<Role> roleSet = roleDao.findByUserId(id);
        if (roleSet != null && roleSet.size() > 0) {
            for (Role role : roleSet) {
                Integer roleId = role.getId();
                Set<Permission> pset = permissionDao.findById(roleId);
                role.setPermissions(pset);
            }
        }
        user.setRoles(roleSet);
        return user;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page = userDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public User findById(int id) {
        return userDao.findByid(id);
    }

    @Override
    public void add(User user, Integer[] roleIds) {
        //先添加到用户表
        userDao.add(user);
        //获取用户id
        Integer userId = user.getId();
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("user_id", userId);
                map.put("role_id", roleId);
                userDao.addUserAndRole(map);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        userDao.deleteAssociation(id);
        userDao.deleteUser(id);

    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        userDao.edit(user);
        //获取用户id
        Integer userId = user.getId();
        userDao.deleteAssociation(userId);
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("user_id", userId);
                map.put("role_id", roleId);
                userDao.addUserAndRole(map);
            }
        }
    }
}